package com.hyh.music.controller;

import com.hyh.music.Result.PageResult;
import com.hyh.music.Result.Result;
import com.hyh.music.constant.MessageConstant;
import com.hyh.music.domain.domainDTO.SongListDTO;
import com.hyh.music.domain.domainDTO.MessagePageDTO;
import com.hyh.music.domain.domains.SongList;
import com.hyh.music.service.SongListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author hyh
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/admin/songList")
public class SongListController {

    @Autowired
    SongListService songListService;

    @GetMapping("/page")
    public Result<PageResult> pageSelect(MessagePageDTO messagePageDTO){
        log.info("歌单的分页查询{}", messagePageDTO);
        PageResult result =  songListService.Page(messagePageDTO);
        return Result.success(result);
    }
    @PostMapping("/addSongList")
    public Result addSongList(@RequestBody SongListDTO songListDTO){
        log.info("添加歌单{}",songListDTO);
        songListService.addSongList(songListDTO);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<SongList> selectById(@PathVariable Integer id ){
        log.info("查询id{}的歌单",id);
        SongList songList = songListService.selectById(id);
        return Result.success(songList);
    }
    @PutMapping("/update")
    public Result updateById(@RequestBody SongList songList){
        log.info("更新{}",songList);
        songListService.update(songList);
        return Result.success();
    }
    @DeleteMapping("/delete")
    public Result deleteById(Integer id){
        log.info("删除id为{}的歌dan",id);
        int delete = songListService.deleteById(id);
        if (delete == MessageConstant.SUCCESS){
            return Result.success();
        }
        else{
            return Result.error("歌单中有歌曲不能删除");
        }

    }

}
